Use the `atty` crate for TTY detection
authorAlex Crichton <alex@alexcrichton.com>
Thu, 15 Jun 2017 15:08:35 +0000 (08:08 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 15 Jun 2017 15:08:35 +0000 (08:08 -0700)
This is more robust in the face of msys terminals and otherwise helps share more
dependencies!

Closes #4166

Cargo.lock
Cargo.toml
src/cargo/core/shell.rs
src/cargo/lib.rs

index 01942aad08d872a17e180375f855883de5fb0bb8..4b464177d10f097ec6fc5dd8dc332967d8d70e63 100644 (file)
@@ -3,6 +3,7 @@ name = "cargo"
 version = "0.21.0"
 dependencies = [
  "advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "bufstream 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "cargotest 0.1.0",
  "crates-io 0.10.0",
@@ -68,6 +69,16 @@ dependencies = [
  "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "atty"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "backtrace"
 version = "0.3.2"
@@ -886,6 +897,7 @@ dependencies = [
 "checksum advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e06588080cb19d0acb6739808aafa5f26bfb2ca015b2b6370028b44cf7cb8a9a"
 "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
 "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
+"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159"
 "checksum backtrace 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72f9b4182546f4b04ebc4ab7f84948953a118bd6021a1b6a6c909e3e94f6be76"
 "checksum backtrace-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3a0d842ea781ce92be2bf78a9b38883948542749640b8378b3b2f03d1fd9f1ff"
 "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"
index d5b4b4254527c19fde1a16cef24891b56bea162c..ca033e571d84ea0ee606e088e0500930c71ca9ea 100644 (file)
@@ -17,6 +17,7 @@ name = "cargo"
 path = "src/cargo/lib.rs"
 
 [dependencies]
+atty = "0.2"
 crates-io = { path = "src/crates-io", version = "0.10" }
 crossbeam = "0.2"
 curl = "0.4.6"
index 02161ff249d508837b3d9fe2e5fedcc8cac76b24..1dfeb375c52da1a34b0f9d3085e17321c25d8c1b 100644 (file)
@@ -1,8 +1,9 @@
 use std::fmt;
 use std::io::prelude::*;
 
-use termcolor::{self, StandardStream, Color, ColorSpec, WriteColor};
+use atty;
 use termcolor::Color::{Green, Red, Yellow};
+use termcolor::{self, StandardStream, Color, ColorSpec, WriteColor};
 
 use util::errors::CargoResult;
 
@@ -138,29 +139,15 @@ impl Shell {
 
 impl ColorChoice {
     fn to_termcolor_color_choice(&self) -> termcolor::ColorChoice {
-        return match *self {
+        match *self {
             ColorChoice::Always => termcolor::ColorChoice::Always,
             ColorChoice::Never => termcolor::ColorChoice::Never,
-            ColorChoice::CargoAuto if isatty() => termcolor::ColorChoice::Auto,
-            ColorChoice::CargoAuto => termcolor::ColorChoice::Never,
-        };
-
-        #[cfg(unix)]
-        fn isatty() -> bool {
-            extern crate libc;
-
-            unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
-        }
-
-        #[cfg(windows)]
-        fn isatty() -> bool {
-            extern crate kernel32;
-            extern crate winapi;
-
-            unsafe {
-                let handle = kernel32::GetStdHandle(winapi::STD_ERROR_HANDLE);
-                let mut out = 0;
-                kernel32::GetConsoleMode(handle, &mut out) != 0
+            ColorChoice::CargoAuto => {
+                if atty::is(atty::Stream::Stderr) {
+                    termcolor::ColorChoice::Auto
+                } else {
+                    termcolor::ColorChoice::Never
+                }
             }
         }
     }
index e2b39cc9244622d3bc696abfe2eef90eb8ce4bc1..3f03697d4dd22cc84b18f3a188f0a6e92a5194b2 100755 (executable)
@@ -8,6 +8,7 @@
 #[macro_use] extern crate scoped_tls;
 #[macro_use] extern crate serde_derive;
 #[macro_use] extern crate serde_json;
+extern crate atty;
 extern crate crates_io as registry;
 extern crate crossbeam;
 extern crate curl;